package org.hdax.dm.service.impl;

import org.hda.dm.utils.DateUtil;
import org.hdax.dm.client.DmImageClient;
import org.hdax.dm.client.DmItemCommentClient;
import org.hdax.dm.entity.image.DmImage;
import org.hdax.dm.entity.itemComment.DmItemComment;
import org.hdax.dm.request.QueryItemComment;
import org.hdax.dm.service.DmItemCommentService;
import org.hdax.dm.vo.CommonResponse;
import org.hdax.dm.vo.VoUtil;
import org.hdax.dm.vo.entity.item.VoQueryItemComment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class DmItemCommentServiceImpl implements DmItemCommentService {

    @Autowired
    private DmItemCommentClient dmItemCommentClient;

    @Autowired
    private DmImageClient dmImageClient;

    @Override
    public CommonResponse<List<VoQueryItemComment>> queryItemComment(QueryItemComment queryItemComment) {
        List<VoQueryItemComment> voQueryItemCommentList = new ArrayList<>();
        if (queryItemComment != null) {
            DmItemComment dmItemComment = new DmItemComment();
            dmItemComment.setItemId(queryItemComment.getId());
            List<DmItemComment> dmItemCommentList = dmItemCommentClient.findDmItemComment(dmItemComment);

            dmItemCommentList.forEach(item -> {
                VoQueryItemComment voQueryItemComment = new VoQueryItemComment();
                BeanUtils.copyProperties(item, voQueryItemComment);
                voQueryItemComment.setCreatedTime(DateUtil.dateToString(item.getCreatedTime()));
                List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(item.getUserId(), 0));
                if (dmImages.size() > 0) {
                    voQueryItemComment.setImgUrl(dmImages.get(0).getImgUrl());
                }
                voQueryItemCommentList.add(voQueryItemComment);
            });
        }
        return VoUtil.returnSuccess(voQueryItemCommentList);
    }

    @Override
    public CommonResponse commitItemComment(DmItemComment dmItemComment) {
        dmItemCommentClient.commitItemComment(dmItemComment);
        return null;
    }
}
